import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);

const store = new Vuex.Store({
    state: {
        // userId: undefined,
        userId: 1,
        username: undefined,
        nickname: undefined
    },
    // 修改全局变量必须使用mutations中的方法
    // mutations 只能采用同步方法
    mutations: {
        login(state, payload) {
            state.userId = payload.id,
            state.username = payload.userName
            state.nickname = payload.nickName

        },
        logout(state) {
            state.userId = undefined;
            state.username = undefined;
            state.nickname = undefined;
        },
        reload(state, payload) {
            state.userId = payload.userId,
            state.username = payload.userName
            state.nickname = payload.nickName
        }
    },
    // 异步方法使用 actions
    // actions 不能直接修改全局变量，需要调用 commit 方法来触发 mutations中的方法
    actions: {
        login(context, payload) {
            context.commit('login', payload)
        },
        logout(context){
            context.commit('logout')
        }
    },
    getters: {},
    modules: {}
});
export default store